package com.ohuang.baseapp.view.refresh.OhRefrsh;

import com.ohuang.baseapp.ResourceTable;
import com.ohuang.baseapp.utils.HiLogUtil;
import com.ohuang.baseapp.utils.ToastUtils;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;


/**
 *
 */
public class OhRefreshLayout extends DirectionalLayout implements Refresh
        , ComponentContainer.EstimateSizeListener, Component.TouchEventListener {
    public OhRefreshLayout(Context context) {
        this(context, null);
    }

    public OhRefreshLayout(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.context = context;
        initView();

    }

    private void initView() {
        topFreshView = new TextRefreshView(context);

        bottomFreshView = new BottomFreshView(context);
        addComponent(topFreshView);
        addComponent(bottomFreshView);

        setEstimateSizeListener(this);
        setTouchEventListener(this);

//        setArrangeListener(this);

    }


    private Context context;
    private Component topFreshView;
    private Component bottomFreshView;
    private ListContainer mTarget; // the target of the gesture
    private int topFreshViewH = 300;//顶部View高度
    private int bottomFreshViewH = 50;//底部View高度
    private int marginTop = -topFreshViewH;//初始化位置

    private boolean canPullDown = true;//是否可以下拉
    private boolean canPullUp = true;//是否可以上拉

    private boolean isPullDown = true;

    private boolean isRefresh = false;


    private int maxPullDownHeight = 500;//最大下拉高度
    private int maxPullUpHeight = 55;//最大上拉高度

    private OnRefreshListener onRefreshListener;
    private float lastY = 0;
    private float lastX = 0;

    private boolean isPressDown = false;


    public OnRefreshListener getOnRefreshListener() {
        return onRefreshListener;
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }


    public boolean isCanPullDown() {
        return canPullDown;
    }

    public void setCanPullDown(boolean canPullDown) {
        this.canPullDown = canPullDown;
    }

    public boolean isCanPullUp() {
        return canPullUp;
    }

    public void setCanPullUp(boolean canPullUp) {
        this.canPullUp = canPullUp;
    }

    public void setMaxPullDownHeight(int height) {
        if (height < 0) {
            height = 0;
        }
        maxPullDownHeight = height;
    }

    public void setMaxPullUpHeight(int height) {
        if (height < 0) {
            height = 0;
        }
        maxPullUpHeight = height;
    }

    public void setTopFreshViewHeight(int height) {
        if (height < 0) {
            height = 0;
        }
        topFreshViewH = height;
        marginTop = -topFreshViewH;
        invalidate();
    }

    public void setBottomFreshViewH(int height) {
        if (height < 0) {
            height = 0;
        }
        bottomFreshViewH = height;
        invalidate();
    }

    public void setTopFreshView(Component view) {
        if (view != null) {
            removeComponent(topFreshView);
            topFreshView = view;
            addComponent(view);
        }
    }

    public void setBottomFreshView(Component view) {
        if (view != null) {
            removeComponent(bottomFreshView);
            bottomFreshView = view;
            addComponent(view);
        }
    }


    @Override
    public void refreshComplete() {
        marginTop = -topFreshViewH;
        marginToTop(marginTop);
        isRefresh = false;
        if (isPullDown) {
            if (topFreshView instanceof RefreshView) {
                ((RefreshView) topFreshView).refreshComplete();
            }
        } else {
            if (bottomFreshView instanceof RefreshView) {
                ((RefreshView) bottomFreshView).refreshComplete();
            }
        }
        postLayout();

    }

    @Override
    public void onRelease() {
        if (isPullDown) {
            if (topFreshView instanceof RefreshView) {
                ((RefreshView) topFreshView).onRelease(this);
            }
        } else {
            if (bottomFreshView instanceof RefreshView) {
                ((RefreshView) bottomFreshView).onRelease(this);
            }
        }
    }

    private boolean targetCanScrollUp() {
        return mTarget.canScroll(DRAG_UP);
    }

    private boolean targetCanScrollDown() {
        return mTarget.canScroll(DRAG_DOWN);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if (component == this) {
            switch (touchEvent.getAction()) {
                case TouchEvent.PRIMARY_POINT_UP:
                    isPressDown = false;
                    touchUp();
                    break;
                case TouchEvent.PRIMARY_POINT_DOWN:
                    isPressDown = false;
                    break;
                case TouchEvent.POINT_MOVE:
                    float y = touchEvent.getPointerPosition(touchEvent.getIndex()).getY();
                    if (isPressDown) {
                        touchMove((int) (y - lastY));
                    }else {
                        isPressDown=true;
                    }

                    lastY = y;
                    break;
                case TouchEvent.CANCEL:
                    break;

            }
        }

        return true;
    }


    private void touchMove(int dy) {
        boolean pullDown = dy > 0 && !targetCanScrollDown() && canPullDown;
        boolean pullUp = dy < 0 && !targetCanScrollUp() && canPullUp;
        boolean canMove = marginTop != -topFreshViewH;
        if (!isRefresh) {
            if (pullDown) {
                isPullDown = true;
            }
            if (pullUp) {
                isPullDown = false;
            }
        }

        if (pullDown || pullUp || canMove) {
            if (!canMove) {
                if (isPullDown) {
                    if (topFreshView instanceof RefreshView) {
                        ((RefreshView) topFreshView).initRefresh();
                    }
                } else {
                    if (bottomFreshView instanceof RefreshView) {
                        ((RefreshView) bottomFreshView).initRefresh();
                    }
                }
            } else {
                if (isPullDown) {
                    mTarget.scrollTo(0, 0);
                } else {
                   
                }

            }
            marginByTop(dy);

        }


    }

    private void touchUp() {
        if (isPullDown) {
            if (marginTop > 0) {
                marginTop = 0;

                if (!isRefresh) {
                    if (topFreshView instanceof RefreshView) {
                        ((RefreshView) topFreshView).onRefresh(this);
                    }
                    if (onRefreshListener != null) {
                        onRefreshListener.onRefresh(this);
                    }
                }
                isRefresh = true;
            } else {
                marginTop = -topFreshViewH;
            }
        } else {
            if (marginTop < -topFreshViewH - bottomFreshViewH) {
                marginTop = -topFreshViewH - bottomFreshViewH;
                if (!isRefresh) {
                    if (bottomFreshView instanceof RefreshView) {
                        ((RefreshView) bottomFreshView).onRefresh(this);
                    }
                    if (onRefreshListener != null) {
                        onRefreshListener.onBottomRefresh(this);
                    }
                }
                isRefresh = true;
            } else {
                marginTop = -topFreshViewH;
            }
        }
        topFreshView.setMarginTop(marginTop);

    }

    private void marginByTop(int dy) {
        marginToTop(marginTop + dy);
    }

    private void marginToTop(int y) {
        if (isPullDown) {
            if (y > maxPullDownHeight - topFreshViewH) {
                y = maxPullDownHeight - topFreshViewH;
            }
            if (y < -topFreshViewH) {
                y = -topFreshViewH;
            }
        } else {
            if (y < -maxPullUpHeight - topFreshViewH) {
                y = -maxPullUpHeight - topFreshViewH;
            }
            if (y > -topFreshViewH) {
                y = -topFreshViewH;
            }
        }
        if (y != marginTop) {
            marginTop = y;
            topFreshView.setMarginTop(marginTop);
        }

    }


    @Override
    public boolean onEstimateSize(int i, int i1) {
        if (mTarget == null) {
            ensureTarget();
        }
        if (mTarget == null) {
            return false;
        }
        if (getChildCount() > 3) {
            throw new RuntimeException("OhRefreshLayout 的子组件超过3个");
        }
        if (getChildIndex(bottomFreshView) != 2) {
            removeComponent(bottomFreshView);
            addComponent(bottomFreshView);
        }

        int with = EstimateSpec.getSize(i);
        int height = EstimateSpec.getSize(i1);

        mTarget.estimateSize(EstimateSpec.getSizeWithMode(
                with - getPaddingLeft() - getPaddingRight(),
                EstimateSpec.PRECISE), EstimateSpec.getSizeWithMode(
                height - getPaddingTop() - getPaddingBottom(), EstimateSpec.PRECISE));
        topFreshView.estimateSize(EstimateSpec.getSizeWithMode(
                with - getPaddingLeft() - getPaddingRight(), EstimateSpec.PRECISE),
                EstimateSpec.getSizeWithMode(topFreshViewH, EstimateSpec.PRECISE));
        bottomFreshView.estimateSize(EstimateSpec.getSizeWithMode(
                with - getPaddingLeft() - getPaddingRight(), EstimateSpec.PRECISE),
                EstimateSpec.getSizeWithMode(bottomFreshViewH, EstimateSpec.PRECISE));

        setEstimatedSize(EstimateSpec.getSizeWithMode(with, EstimateSpec.PRECISE)
                , EstimateSpec.getSizeWithMode(height, EstimateSpec.PRECISE));

        topFreshView.setMarginTop(marginTop);
        return true;
    }

    private void ensureTarget() {
        // Don't bother getting the parent height if the parent hasn't been laid
        // out yet.
        if (mTarget == null) {
            for (int i = 0; i < getChildCount(); i++) {
                Component child = getComponentAt(i);
                if (!child.equals(topFreshView)) {
                    if (!child.equals(bottomFreshView)) {
                        mTarget = (ListContainer) child;
                        break;
                    }
                }
            }
        }
    }


}
